﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;


namespace AForge_zxing
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        FilterInfoCollection videoDevices;
        VideoCaptureDevice videoSource;
        public int selectedDeviceIndex = 0;
        bool DeviceExist;
        int top = 0;
        Bitmap img = null;

        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            img = (Bitmap)eventArgs.Frame.Clone();
        }

        private void CloseVideoSource()
        {
            if (!(videoSource == null))
                if (videoSource.IsRunning)
                {
                    videoSource.SignalToStop();
                    videoSource = null;
                }
        }


        private void getCamList()
        {
            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                comboBox1.Items.Clear();
                if (videoDevices.Count == 0)
                    throw new ApplicationException();
                DeviceExist = true;
                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);
                }
                comboBox1.SelectedIndex = 0;
            }

            catch (ApplicationException)
            {
                DeviceExist = false;
                comboBox1.Items.Add("未找到可用设备");
            }
        }

        [Obsolete]
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "开始")
            {
                if (DeviceExist)
                {
                    videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                    videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                    CloseVideoSource();
                    videoSource.DesiredFrameSize = new Size(1920,1080);
                    videoSource.Start();
                    button1.Text = "结束";
                    timer1.Enabled = true;
                    timer2.Enabled = true;
                }
            }
            else
            {
                if (videoSource.IsRunning)
                {
                    timer2.Enabled = false;
                    timer1.Enabled = false;
                    CloseVideoSource();
                    button1.Text = "开始";
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (img == null)
            {
                return;
            }
            #region
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bt = ms.GetBuffer();
            ms.Close();
            #endregion
            #region
            LuminanceSource source = new RGBLuminanceSource(bt, img.Width, img.Height);
            BinaryBitmap bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source));
            Result result;
            MultiFormatReader multiFormatReader = new MultiFormatReader();

            try
            {
                result = multiFormatReader.decode(bitmap);
            }
            catch (Exception ex)
            {
                return;
            }
            finally
            {
                multiFormatReader.reset();
            }
            if (result != null)
            {
                textBox1.Text = result.Text;
            }
            #endregion
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (img == null)
            {
                return;
            }
            Bitmap img2 = (Bitmap)img.Clone();
            Pen p = new Pen(Color.Red);
            Graphics g = Graphics.FromImage(img2);
            Point p1 = new Point(0, top);
            Point p2 = new Point(pictureBox1.Width, top);
            g.DrawLine(p, p1, p2);
            g.Dispose();
            top += 2;
            top = top % pictureBox1.Height;
            pictureBox1.Image = img2;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            getCamList();
        }

        public Form1(String ISBN)
            : this()
        {
            textBox1.Text = ISBN;
        }

        public string reisbn
        {
            get {return textBox1.Text; }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //if (videoSource.IsRunning)
            //{
                timer2.Enabled = false;
                timer1.Enabled = false;
                CloseVideoSource();
            //}
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (videoSource.IsRunning)
            //{
                timer2.Enabled = false;
                timer1.Enabled = false;
                CloseVideoSource();
            //}
            //this.Close();
            //MessageBox.Show(this.Text);
            //this.Hide();

        }
    }
}
